package top.codehelen.common.response;

import top.codehelen.common.constants.ReturnCodeEnum;

public class R<T> {
    private long code;
    private String msg;
    private T data;

    protected R() {
    }

    protected R(long code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }


    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> R<T> success(T data) {
        return new R<T>(ReturnCodeEnum.SUCCESS.getCode(), ReturnCodeEnum.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> success() {
        return new R<T>(ReturnCodeEnum.SUCCESS.getCode(), ReturnCodeEnum.SUCCESS.getMsg(), null);
    }


    public static <T> R<T> success(String msg) {
        return new R<T>(ReturnCodeEnum.SUCCESS.getCode(), msg, null);
    }


    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> R<T> success(T data, String message) {
        return new R<T>(ReturnCodeEnum.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> R<T> failed(ReturnCodeEnum returnCodeEnum) {
        return new R<T>(returnCodeEnum.getCode(), returnCodeEnum.getMsg(), null);
    }

    public static <T> R<T> failedWithArgument(String msg) {
        return new R<T>(ReturnCodeEnum.INCORRECT_ARGUMENT.getCode(), msg, null);
    }

    public static <T> R<T> failedWithArgument() {
        return new R<T>(ReturnCodeEnum.INCORRECT_ARGUMENT.getCode(), ReturnCodeEnum.INCORRECT_ARGUMENT.getMsg(), null);
    }

    /**
     * 失败返回结果
     * @param message 错误信息
     */
    public static <T> R<T> failed(ReturnCodeEnum returnCodeEnum, String message) {
        return new R<T>(returnCodeEnum.getCode(), message, null);
    }

    public static <T> R<T> failed(ReturnCodeEnum returnCodeEnum, T data) {
        return new R<T>(returnCodeEnum.getCode(), returnCodeEnum.getMsg(), data);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> R<T> failed(String message) {
        return new R<T>(ReturnCodeEnum.SYSTEM_ERROR.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> R<T> failed() {
        return failed(ReturnCodeEnum.SYSTEM_ERROR);
    }


    public void setCode(long code) {
        this.code = code;
    }

    public long getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
